<?php
session_start();
include 'inc/db_connect.php';
include 'inc/db_op.php';
include 'inc/html_op.php';
include 'inc/form_op.php';
date_default_timezone_set('Asia/Chongqing');

$page_title = "Summary";
$debug = 0;

$today = strtotime("today");

function get_wlog($user_id, $day) {
  $html_str = "";
  $mysql_date = date('Y-m-d H:i:s', $day);

  $common_where = " to_days(end_time) = to_days('$mysql_date') and user_id = $user_id ";
  $order = " order by last_modified";
  
  $sql = "select task_info.title task_title, task_info.is_trip_out is_trip_out, ".
         "task_type.name task_type_name, work_log.title wlog_title, work_log.content wlog_content, ".
         "updated_progress, work_hours, overtime_hours ".
         "from work_log left join task_info on work_log.task_id = task_info.id left join ".
         "task_type on task_info.type_id = task_type.id where ".
         $common_where.$where;
    
  $wlogs = db_get($sql);
  $list_items = array();
  foreach($wlogs as $wlog) {
    $task_str = '';
    if($wlog['task_title'] != "") {
      $task_str .= '{'.$wlog['task_type_name'].'}'.$wlog['task_title'];
    }
    $wlog_str = $wlog['wlog_title'];
    $wlog_str .= '100' == $wlog['updated_progress'] ? '【完成】' : '';
    $wlog_str .= $wlog['overtime_hours']>0 ? '（加班）' : '';
    
    $tip_str = $wlog['wlog_content'].'<br/>耗时：'.$wlog['work_hours'].'小时';
    $tip_str .= $wlog['overtime_hours']>0 ? '<br/>加班：'.$wlog['overtime_hours'].'小时' : '';
    $list_items[gen_sep_str(array($task_str, $wlog_str),'；')] = $tip_str;
  }
  
  $html_str .= gen_list_with_tip($list_items, 'ul');
  
  $trip_location_sql = "select distinct customer_location from ".
                       " work_log left join task_info on work_log.task_id = task_info.id ".
                       " left join project_info on task_info.project_id = project_info.id ".
                       " where is_trip_out = 1 and ".
                       $common_where;
  //$html_str .= $trip_location_sql;
  $trip_locations = db_get($trip_location_sql);
  if(count($trip_locations) > 0) {
    $location_array = array();
    foreach($trip_locations as $trip_location) {
      array_push($location_array, $trip_location['customer_location']);
    }
    $html_str .= '<p style="text-indent:2em">出差'.gen_sep_str($location_array).'。</p>';
  }
  
  return $html_str;
}

function get_todos($user_id) {
  $html_str = "";
  $sql = "select title, content, priority from task_info where progress < 100 and assignto = $user_id ".
         ' order by priority ';
         
  $todos = db_get($sql);
  $list_items = array();
  foreach($todos as $todo) {
    $disp_str = (0 == strcmp($todo['priority'],'高') ? '！' : '');
    $disp_str .= $todo['title'];
    $list_items[$disp_str] = $todo['content'];
  }
  
  $html_str .= gen_list_with_tip($list_items, 'ul');
  
  return $html_str;
}

function gen_pre_next_link($end_day) {
  $pre_end_day = strtotime('last Sunday', $end_day);
  $pre_link_href = "javascript:submit_a_value('end_day', '$pre_end_day')";
  $pre_link = '<a href="'.$pre_link_href.'">前一周</a>';
  $next_link = "";
  $this_week_link = "";
  if($end_day < $GLOBALS['today']) {
    $next_end_day = strtotime('next Sunday', $end_day);
    $next_link_href = "javascript:submit_a_value('end_day', '$next_end_day')";
	$next_link .= ' &nbsp &nbsp <a href="'.$next_link_href.'">后一周</a>';
	if($next_end_day < $GLOBALS['today']) {
	  $this_week_link_href = "javascript:submit_a_value('','')";
	  $this_week_link .= ' &nbsp &nbsp <a href="'.$this_week_link_href.'">本周</a>';
	}
  }
 
  return "<p>$pre_link $next_link $this_week_link</p>";
}

function get_summary($begin_day, $end_day) {
  $html_str = "";
  $chn_wday = array("日","一","二","三","四","五","六");

  $html_str .= '<h2>'.$_SESSION['department_name'].'工作日志汇总（'.date("Y.n.j", $begin_day).'-'.date("Y.n.j", $end_day).'）</h2>';
  $html_str .= gen_pre_next_link($end_day);
  $html_str .= '<table border=1><tr><th width=5%>姓&nbsp&nbsp名</th>';
  $days = range($begin_day, $end_day, 3600*24);
  foreach($days as $day){
    $wday = date("w", $day);
    $width = 90;
    $html_str .= "<th width=$width>".date("n.j",$day)."<br/>星期".$chn_wday[$wday].'</th>';
  }
  $html_str .= '</tr>';

  $sql = 'select name, id from user_info where (disabled = 0 or disabled is null) and department_id = '.$_SESSION['department_id'].' order by display_order';
  $users = db_get($sql);
  foreach($users as $user) {
    $html_str .= '<tr>';
    $html_str .= '<td aligh = "center"><b>'.$user['name'].'</b></td>';
    
    $user_id = $user['id'];
    
    foreach($days as $day) {
      $html_str .= '<td>'.get_wlog($user_id, $day).'</td>';
    }
    
    // $html_str .= '<td>'.get_todos($user_id).'</td>';
    
    $html_str .= '</tr>';
  }

  $html_str .= '</table>';

  return $html_str;
}

if(isset($_POST['end_day'])) {
  $end_day = $_POST['end_day'];
} else {
  $end_day = $today;
}
$begin_day = strtotime('last Monday', strtotime('this Sunday', $end_day));

$content = get_summary($begin_day, $end_day);
echo gen_onecol_page($page_title, $content, $debug);

?>